import enum
import json
import requests
import mysql.connector
from mysql.connector import Error
from bs4 import BeautifulSoup

# 配置参数
headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Origin': 'http://gzh.zjxlx.org.cn',
        'Proxy-Connection': 'keep-alive',
        'Referer': 'http://gzh.zjxlx.org.cn/XtNet/Que',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    
cookies = {
    'ASP.NET_SessionId': 'v1qwnf5cgukzf1ggaamyjfry'
}

# SimType
class SimType(enum.Enum):
    """题目类型枚举"""
    SINGLE_CHOICE = 'single_choice'  # 单选题
    MULTIPLE_CHOICE = 'multiple_choice'  # 多选题
    TRUE_FALSE = 'true_false'  # 判断题
    SCALE = 'scale'  # 量表题
    TEXT = 'text'  # 文本题

# 抓取hmtl页面id=option的所有选项
def get_options(id):
    url = f'http://gzh.zjxlx.org.cn/XtNet/Subject/Edit/?id={id}'
    
    response = requests.get(url, headers=headers, cookies=cookies)
    if response.status_code == 200:
        html = response.text
        # print(html)
        soup = BeautifulSoup(html, 'html.parser')
        option_element = soup.find(id='option')
        if option_element:
            # 查找所有option div元素
            result = []
            option_divs = option_element.find_all('div', class_='option')
            for i, option_div in enumerate(option_divs):
                # 获取选项内容
                name_input = option_div.find('input', {'name': lambda x: x and 'Name' in x})
                score_input = option_div.find('input', {'name': lambda x: x and 'OptScore' in x})
                sno_input = option_div.find('input', {'name': lambda x: x and 'Sno' in x})
                content_textarea = option_div.find('textarea', {'name': lambda x: x and 'OptContent' in x})
                
                name = name_input.get('value') if name_input else ''
                score = score_input.get('value') if score_input else ''
                sno = sno_input.get('value') if sno_input else ''
                content = content_textarea.text if content_textarea else ''
                
               
                result.append({
                    'label': name,
                    'value':score,
                    'order': sno
                })
            # print(result)
            return result
        else:
            print("未找到id为'option'的元素")


def fetch_data_from_api(page=1, per_page=180):
    """从API抓取数据"""
    url = 'http://gzh.zjxlx.org.cn/XtNet/Subject/Lists'
    
    data = {
        'page': str(page),
        'limit': str(per_page)
    }
    
    try:
        response = requests.post(url, headers=headers, cookies=cookies, data=data, verify=False)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"请求API时出错: {e}")
        return None

def create_mysql_connection():
    """创建MySQL数据库连接"""
    try:
        connection = mysql.connector.connect(
            host='114.55.93.104',
            database='psychology_test',  # 请修改为您的数据库名
            user='psychology_test',           # 请修改为您的用户名
            password='123456'        # 请修改为您的密码
        )
        return connection
    except Error as e:
        print(f"连接MySQL数据库时出错: {e}")
        return None


def save_data_to_mysql(connection, data):
    """将数据保存到MySQL数据库"""
    if not data or 'data' not in data:
        print("没有有效数据需要保存")
        return 0
    
    insert_query = """
    INSERT INTO questions (
        assessment_id, title,question_type, score, options
    ) VALUES (
        %s, %s,  %s, %s,  %s
    )
    """
    try:
        cursor = connection.cursor()
        count = 0
        # 根据API返回的数据结构处理数据
        for item in data.get('data', []):
            Xid=item.get('Xid')
            TabVal = item.get('TabVal')

            options = get_options(Xid)
            score = 0
            for v in options:
                v['value'] = int(float(v['value']))
                if v['value'] > score:
                    score = v['value']
        
            assessment_id = cursor.execute("SELECT id FROM assessments WHERE code = %s", (TabVal,))
            assessment_id = cursor.fetchone()
            if not assessment_id:
                print(f"未找到assessment_id: {TabVal}")
                continue
            assessment_id = assessment_id[0]
            if assessment_id == 2:
                print('跳过assessment_id=2，scl-90')
                continue
            title = item.get('Title')
            question_type = 'single_choice'
            score = score
            options = json.dumps(options)
            
            # 检查是否已存在相同assessment_id和title的记录
            cursor.execute("SELECT id FROM questions WHERE assessment_id = %s AND title = %s", (assessment_id, title))
            existing_record = cursor.fetchone()
            
            if existing_record:
                print(f"跳过已存在的记录: assessment_id={assessment_id}, title={title}")
                continue
            
            # 不存在则插入新数据
            cursor.execute(insert_query, (
                assessment_id, title, question_type, score, options
            ))
            count += 1
            print(f'assessment_id>>{count}',title,TabVal,assessment_id)
        
        connection.commit()
        print(f"成功处理 {count} 条数据")
        return count
        
    except Error as e:
        print(f"处理数据时出错: {e}")
        connection.rollback()
        return 0

def main():
    """主函数"""
    print("开始从API抓取数据...")
    
    # 连接数据库
    connection = create_mysql_connection()
    if not connection:
        print("数据库连接失败")
        return
    
    try:
        # 从第5页开始循环抓取数据，直到没有数据为止
        page = 1
        per_page = 100
        total_count = 0
        
        while True:
            print(f"正在抓取第 {page} 页数据...")
            data = fetch_data_from_api(page=page, per_page=per_page)
            
            if not data or 'data' not in data or not data['data']:
                print(f"第 {page} 页没有数据，抓取完成")
                break
            
            # 保存当前页数据
            count = save_data_to_mysql(connection, data)
            total_count += count
            print(f"第 {page} 页处理完成，共 {count} 条数据")
            
            # 继续抓取下一页
            page += 1
            
        print(f"所有数据抓取完成，共处理 {total_count} 条数据")
        
    finally:
        if connection.is_connected():
            connection.close()
            print("数据库连接已关闭")

if __name__ == "__main__":
    main()
    # get_options(5472)